/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#ifndef LASERSCANNER_H
#define LASERSCANNER_H

#include "Object.h"
#include "Shapes.h"
#include "RayCluster.h"
#include "../MaCI/interfaces/Ranging/RangingServer.hpp"
#include "../MaCI/interfaces/Ranging/RangingTypes.hpp"
#include "binbag.h"



/*
Laser scanner model.
Uses RayCluster primitive and has its own collision detection for rays.
Rays are collided only when needed, that is, GetReading is called.
*/

struct TRangingData
{
  float angle;
  float distance;
};

class CLaserScanner : public CObject
{
  public:
    CLaserScanner(CWorld *World, CObject *ParentObject, dBodyID parentBody,
                  dReal *posOffset, dMatrix3 rotOffset, float interval, 
                  unsigned int numOfRays, float range, float minAngle, 
                  float maxAngle, /*gimi::GIMI* gimi*/MaCI::MaCICtrl::CMaCICtrlServer *mcs,
                  int minor, std::string interfaceName="Laser");

    ~CLaserScanner();

    void Draw();

    void Activate();
    void Deactivate();

    void Update(float StepSize);

    void OnCollision(CPrimitive *primitive, int numc, dContact *contacts, CPrimitive *primitive2);
    
    //gets reading and sets rangingServerData redy for sending;
    void GetReading();

    void SetDevicePosition(float x, float y, float z, float heading);
		
		void setRayClusterVisible(bool isVisible){rayCluster->IsVisible = isVisible;}
		
  protected:
    dSpaceID RaySpaceID;
    /*
    TMaterial material;
    CBox box, box2;
    */
		CRayCluster *rayCluster;

    dReal *PosOffset;
    dMatrix3 RotOffset;

    float interval;
    float timer;
    //gimi::GIMI* gimi;
    gim::binbag::CBinBag binbag;
    //MaCI::MaCICtrl::CMaCICtrlServer mcs;
    MaCI::Ranging::CRangingData rangingServerData;
    MaCI::Ranging::CRangingServer rangingServer;
    
    unsigned int numberOfRays;
    float range;
    
    std::vector<TRangingData> rangingData;

    //collision callback for rays
    static void RayCallback(void *data, dGeomID o1, dGeomID o2);

};

#endif //LASERSCANNER_H
